package cn.lee.cplibrary.widget.richet.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import cn.lee.cplibrary.R;
import cn.lee.cplibrary.init.CPInitializer;
import cn.lee.cplibrary.widget.richet.bean.FontStyle;

/**
 * Created by awarmisland on 2018/9/10.
 */
public class FontSizeSelectView extends LinearLayout implements View.OnClickListener {
    Button btn_font_size_small;
    Button btn_font_size_normal;
    Button btn_font_size_big;

    private OnFontSizeChangeListener onFontSizeChangeListener;
    private FontStyle fontStyle;

    public FontSizeSelectView(Context context) {
        super(context);
        initView(context);
    }

    public FontSizeSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public FontSizeSelectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        CPInitializer.checkInitialization();
        View view = LayoutInflater.from(context).inflate(R.layout.cp_view_font_size_select, this);
        btn_font_size_small = view.findViewById(R.id.btn_font_size_small);
        btn_font_size_normal = view.findViewById(R.id.btn_font_size_normal);
        btn_font_size_big = view.findViewById(R.id.btn_font_size_big);
        btn_font_size_small.setOnClickListener(this);
        btn_font_size_normal.setOnClickListener(this);
        btn_font_size_big.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_font_size_small) {
            if (onFontSizeChangeListener != null) {
                fontStyle.fontSize = FontStyle.SMALL;
                onFontSizeChangeListener.onFontSizeSelect(FontStyle.SMALL);
                initDefaultView(R.id.btn_font_size_small);
            }
        } else if (v.getId() == R.id.btn_font_size_normal) {
            if (onFontSizeChangeListener != null) {
                fontStyle.fontSize = FontStyle.NORMAL;
                onFontSizeChangeListener.onFontSizeSelect(FontStyle.NORMAL);
                initDefaultView(R.id.btn_font_size_normal);
            }
        } else if (v.getId() == R.id.btn_font_size_big) {
            if (onFontSizeChangeListener != null) {
                fontStyle.fontSize = FontStyle.BIG;
                onFontSizeChangeListener.onFontSizeSelect(FontStyle.BIG);
                initDefaultView(R.id.btn_font_size_big);
            }
        }
    }

    /**
     * 初始化view
     *
     * @param fontStyle
     */
    public void initFontStyle(FontStyle fontStyle) {
        this.fontStyle = fontStyle;
        int size = fontStyle.fontSize;
        long id = R.id.btn_font_size_normal;
        if (size == FontStyle.SMALL) {
            id = R.id.btn_font_size_small;
        } else if (size == FontStyle.BIG) {
            id = R.id.btn_font_size_big;
        }
        initDefaultView(id);
    }

    private void initDefaultView(long id) {
        btn_font_size_small.setTextColor(Color.BLACK);
        btn_font_size_normal.setTextColor(Color.BLACK);
        btn_font_size_big.setTextColor(Color.BLACK);
        if (R.id.btn_font_size_small == id) {
            btn_font_size_small.setTextColor(Color.RED);
        } else if (R.id.btn_font_size_normal == id) {
            btn_font_size_normal.setTextColor(Color.RED);
        } else if (R.id.btn_font_size_big == id) {
            btn_font_size_big.setTextColor(Color.RED);
        }
    }

    public void setOnFontSizeChangeListener(OnFontSizeChangeListener onFontSizeChangeListener) {
        this.onFontSizeChangeListener = onFontSizeChangeListener;
    }

    public void setFontStyle(FontStyle fontStyle) {
        this.fontStyle = fontStyle;
    }


    public interface OnFontSizeChangeListener {
        void onFontSizeSelect(int size);
    }
}
